package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/region")
@Controller
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;


    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") RegionQuery qo){
        //根据请求查询分页数据
        Page<Region> page = regionService.query(qo);
        //加到模型中
        model.addAttribute("page", page);
        return "region/list";
    }

    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(){
        //查询出所有地区数据,返回到ajax
        List<Destination> list = destinationService.selectAll();
        return list;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        //调用服务层方法
        if (StringUtils.hasLength(region.getId())){
            //调用更新方法
            regionService.update(region);
        }else {
            //调用保存方法
            regionService.save(region);
        }
        //返回执行成功
        return JsonResult.success();
    }

    /**
     * 根据id查下面的目的地们
     * @param rid
     * @return
     */
    @RequestMapping("getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        //调用目的地Service根据区域Id对应的所有目的地
        List<Destination> list = destinationService.selectDestsByReginoId(rid);
        //返回
        return list;
    }

    @RequestMapping("changeHotValue")
    @ResponseBody
    public Object changeHotValue(Integer hot,String id){
        //调将特定区域改变热点状态
        regionService.changeHotById(hot,id);
        //返回jsonresult
        return JsonResult.success();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        //调将特定区域改变热点状态
        regionService.deleteById(id);
        //返回jsonresult
        return JsonResult.success();
    }
}
